﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class WebUserControl_TraniningReport : System.Web.UI.UserControl
{
   
   
    string error = "";
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
               
                BindDrop();
                if (Session["usertype"].ToString()!="ad")
                {
                    dr_TTPP.SelectedValue = Session["sitecode"].ToString();
                    dr_TTPP.Enabled = false;
                }
               
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
        else
            try
            {
                if (ViewState["db"] != null)
                {
                    DataTable db = (DataTable)ViewState["db"];
                    ASPxPivotGrid1.DataSource = db;
                    ASPxPivotGrid1.DataBind();
                }
            }
            catch
            {

                return;
            }
    }
    void BindDrop()
    {
        ClassExecute.ClassExecute dtEx = new ClassExecute.ClassExecute();
        DataTable db = dtEx.Get_Data_DataTable(str, ref error, "k_GetTTPP1", CommandType.StoredProcedure);
        dr_TTPP.DataSource = db;
        dr_TTPP.DataTextField = "sitename";
        dr_TTPP.DataValueField = "Sitecode";
        dr_TTPP.DataBind();
    }
    protected void baocao_Click(object sender, EventArgs e)
    {
        try
        {
            ClassExecute.ClassExecute dtEx = new ClassExecute.ClassExecute();
           DataTable  db = dtEx.Get_Data_DataTable(str, ref error, "k_getSaleSPPastTrainingCourse", CommandType.StoredProcedure,
                 new SqlParameter("@sitecode", dr_TTPP.SelectedValue.ToString()));
            if (db.Rows.Count > 0)
            {
                ViewState["db"] = db;
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {
            string contentType = "application/ms-excel";
            string fileName = "Training Report.xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
}
